package net.sx.minemodule.widget;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;

import com.alibaba.android.arouter.launcher.ARouter;
import com.homegoods.aroutercomment.ARouterPath;
import com.kedll.library.adapter.MultiTypeAdapter;
import com.kedll.library.util.BindingUtil;
import com.kedll.library.util.DividerListItemDecoration;

import net.sx.minemodule.R;
import net.sx.minemodule.databinding.MineItemMineOrderBinding;
import net.sx.minemodule.mvp.bean.GoodsBean;
import net.sx.minemodule.mvp.bean.OrderBean;
import net.sx.minemodule.mvp.item.GoodsItem;

import java.util.List;

/**
 * @author 11470
 */
public class MineOrderGoodView extends LinearLayout {

    private MineItemMineOrderBinding mBinding;

    private MultiTypeAdapter adapter = new MultiTypeAdapter();
    private OrderBean mModel;

    public MineOrderGoodView(Context context) {
        this(context, null);
    }

    public MineOrderGoodView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MineOrderGoodView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    private void initView(Context context) {
        mBinding = DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.mine_item_mine_order, this, true);
        LinearLayoutManager layoutManager = new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false);
        mBinding.listMineOrder.setLayoutManager(layoutManager);
        mBinding.listMineOrder.setNestedScrollingEnabled(false);
        mBinding.listMineOrder.setHasFixedSize(true);
        mBinding.listMineOrder.addItemDecoration(new DividerListItemDecoration(context, DividerListItemDecoration.VERTICAL_LIST, R.drawable.drawable_line));
        mBinding.listMineOrder.setAdapter(adapter);
    }

    public void setData(OrderBean model) {
        mModel = model;
        setHeadImage(model.getStoreImgPath());
        setStoreName(model.getStoreName());
        setOrderType(model.getOrderType());

        List<GoodsBean> dataList = model.getGoodsBeanList();
        for (GoodsBean bean : dataList) {
            GoodsItem item = bean.createItem(adapter);
            adapter.addItem(item);
        }

        setAllCount(model.getGoodsCount());
        setAllMoney(model.getGoodsAllMoney());
//[待支付|0,已支付|1,已发货|2,已签收|3,确认收货|4,发起退款|5,退款成功|6]
        switch (model.getOrderType()) {
            case "待支付":
                mBinding.tvRightButtonOrderMine.setTag(0);
                mBinding.tvRightButtonOrderMine.setText("付款");
                mBinding.tvRightButtonOrderMine.setBackgroundResource(R.drawable.mine_bg_2_a541c9);
                mBinding.tvRightButtonOrderMine.setTextColor(getResources().getColor(R.color.color_a541c9));
                mBinding.tvLeftButtonOrderMine.setText("取消订单");
                mBinding.tvLeftButtonOrderMine.setBackgroundResource(R.drawable.mine_bg_2_eeeeee);
                mBinding.tvLeftButtonOrderMine.setTextColor(getResources().getColor(R.color.color_333333));
                mBinding.tvLeftButtonOrderMine.setTag(1);
                break;
            case "已支付":
                mBinding.tvLeftButtonOrderMine.setTag(2);
                mBinding.tvLeftButtonOrderMine.setVisibility(GONE);
                mBinding.tvRightButtonOrderMine.setBackgroundResource(R.drawable.mine_bg_2_eeeeee);
                mBinding.tvRightButtonOrderMine.setText("联系客服");
                break;
            case "已发货":
                mBinding.tvRightButtonOrderMine.setTag(3);
                mBinding.tvRightButtonOrderMine.setText("确认收货");
                mBinding.tvRightButtonOrderMine.setBackgroundResource(R.drawable.mine_bg_2_a541c9);
                mBinding.tvRightButtonOrderMine.setTextColor(getResources().getColor(R.color.color_a541c9));
                mBinding.tvLeftButtonOrderMine.setText("查看物流");
                mBinding.tvLeftButtonOrderMine.setBackgroundResource(R.drawable.mine_bg_2_eeeeee);
                mBinding.tvLeftButtonOrderMine.setTextColor(getResources().getColor(R.color.color_333333));
                mBinding.tvLeftButtonOrderMine.setTag(4);
                break;
            case "确认收货":
            case "已签收":
                mBinding.tvRightButtonOrderMine.setTag(5);
                mBinding.tvRightButtonOrderMine.setText("评价");
                mBinding.tvRightButtonOrderMine.setBackgroundResource(R.drawable.mine_bg_2_a541c9);
                mBinding.tvRightButtonOrderMine.setTextColor(getResources().getColor(R.color.color_a541c9));
                mBinding.tvLeftButtonOrderMine.setText("查看物流");
                mBinding.tvLeftButtonOrderMine.setBackgroundResource(R.drawable.mine_bg_2_eeeeee);
                mBinding.tvLeftButtonOrderMine.setTextColor(getResources().getColor(R.color.color_333333));
                mBinding.tvLeftButtonOrderMine.setTag(4);
                mBinding.tvLeftThreeOrderMine.setVisibility(VISIBLE);
                mBinding.tvLeftThreeOrderMine.setText("取消订单");
                mBinding.tvLeftThreeOrderMine.setBackgroundResource(R.drawable.mine_bg_2_eeeeee);
                mBinding.tvLeftThreeOrderMine.setTextColor(getResources().getColor(R.color.color_333333));
                mBinding.tvLeftThreeOrderMine.setTag(1);
                mBinding.tvLeftFourOrderMine.setVisibility(VISIBLE);
                mBinding.tvLeftFourOrderMine.setText("更多");
                mBinding.tvLeftFourOrderMine.setTextColor(getResources().getColor(R.color.color_333333));
                mBinding.tvLeftFourOrderMine.setTag(6);
                break;
            case "发起退款":
            case "退款成功":
                mBinding.tvRightButtonOrderMine.setText("再次购买");
                mBinding.tvRightButtonOrderMine.setTag(7);
                mBinding.tvRightButtonOrderMine.setBackgroundResource(R.drawable.mine_bg_2_a541c9);
                mBinding.tvRightButtonOrderMine.setTextColor(getResources().getColor(R.color.color_a541c9));
                mBinding.tvLeftButtonOrderMine.setText("取消订单");
                mBinding.tvLeftButtonOrderMine.setBackgroundResource(R.drawable.mine_bg_2_eeeeee);
                mBinding.tvLeftButtonOrderMine.setTextColor(getResources().getColor(R.color.color_333333));
                mBinding.tvLeftButtonOrderMine.setTag(1);
                break;
            default:
                break;
        }
        mBinding.tvRightButtonOrderMine.setOnClickListener(new Listener());
        mBinding.tvLeftButtonOrderMine.setOnClickListener(new Listener());
        mBinding.tvLeftThreeOrderMine.setOnClickListener(new Listener());
        mBinding.tvLeftFourOrderMine.setOnClickListener(new Listener());
    }

    class Listener implements View.OnClickListener {

        @Override
        public void onClick(View view) {
            switch ((Integer) view.getTag()) {
                case 0://付款
                    ARouter.getInstance().build(ARouterPath.PATH_MINE_ORDER_PAY).withInt("type", 0).withString("price", mModel.getGoodsAllMoney()).withString("orderSid", mModel.getTsid()).navigation();
                    break;
                case 1://取消订单

                    break;
                case 2://联系客服

                    break;
                case 3://确认收货

                    break;
                case 4://查看物流

                    break;
                case 5://评价
                    //TODO 接口有问题
                    ARouter.getInstance().build(ARouterPath.PATH_MINE_PUSHSCORE).withString("sid", "").navigation();
                    break;
                case 6://更多

                    break;
                case 7://再次购买

                    break;
                default:
                    break;
            }
        }
    }

    /**
     * 设置头像
     *
     * @param headUrl
     */
    private void setHeadImage(String headUrl) {
        BindingUtil.loadCircleImage(mBinding.ivStoreImgMineOrder, headUrl, null);
    }

    /**
     * 设置店铺名
     *
     * @param storeName
     */
    private void setStoreName(String storeName) {
        mBinding.tvStoreNameMineOrder.setText(storeName);
    }

    /**
     * 订单类型
     *
     * @param orderType
     */
    private void setOrderType(String orderType) {
        mBinding.tvOrderType.setText(orderType);
    }

    /**
     * 共计商品
     *
     * @param allGoods
     */
    private void setAllCount(int allGoods) {
        mBinding.tvCountGoodsOrderMine.setText(String.format("共%d件商品", allGoods));
    }

    /**
     * 合计
     *
     * @param money
     */
    private void setAllMoney(String money) {
        mBinding.tvTotalAllOrderMine.setText("合计: ¥" + money);
    }

}
